package wb.is.besti.edu.cd.emergency_responsesys.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.view.menu.ActionMenuItemView;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import wb.is.besti.edu.cd.emergency_responsesys.R;
import wb.is.besti.edu.cd.emergency_responsesys.ui.MoreNewsActivity;
import wb.is.besti.edu.cd.emergency_responsesys.utils.News;

public class NewsRecyclerAdapter extends RecyclerView.Adapter<NewsRecyclerAdapter.NewsViewHoler> {

    private List<News> newsList;
    private Context context;

    public NewsRecyclerAdapter(List<News> newsList,Context context){
        this.newsList = newsList;
        this.context = context;
    }

    static class NewsViewHoler extends RecyclerView.ViewHolder{
        CardView cardView;
        ImageView news_photo;
        TextView news_title;
        TextView news_desc;
        Button share;
        Button readMore;

        public NewsViewHoler(final View itemView){
            super(itemView);
            cardView = (CardView)itemView.findViewById(R.id.card_view);

            news_desc=(TextView)itemView.findViewById(R.id.news_desc);
            share=(Button)itemView.findViewById(R.id.btn_share);
            readMore=(Button)itemView.findViewById(R.id.btn_more);
            news_title.setBackgroundColor(Color.argb(20,0,0,0));
        }
    }

    @NonNull
    @Override
    public NewsViewHoler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.news_item,parent,false);
        NewsViewHoler newsViewHoler = new NewsViewHoler(view);
        return newsViewHoler;
    }

    @Override
    public void onBindViewHolder(@NonNull NewsViewHoler holder, final int position) {
        final int index = position;
        holder.news_photo.setImageResource(newsList.get(index).getPhotoId());
        holder.news_title.setText(newsList.get(index).getTitle());
        holder.news_desc.setText(newsList.get(index).getDesc());

        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context,MoreNewsActivity.class);
                intent.putExtra("News",newsList.get(index));
                context.startActivity(intent);
            }
        });

        holder.share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
                intent.putExtra(Intent.EXTRA_TEXT, newsList.get(index).getDesc());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(Intent.createChooser(intent, newsList.get(index).getTitle()));
            }
        });
        holder.readMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context,MoreNewsActivity.class);
                intent.putExtra("News",newsList.get(index));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }



}
